/*
 * Author: 李波（Edwin Lee)
 * Date: 2024-06-15 11:04:54
 * FilePath: /chat/include/server/chatservice.h
 * LastEditTime: 2024-06-27 19:43:05
 * version: 1.0
 * Description: 服务方法,单例模式
 */
#include "usermodel.h"
#include "offlinemessagemodel.h"
#include "friendmodel.h"
#include "groupmodel.h"
#include "redis.h"
#include "json.hpp"
using json = nlohmann::json;

#include <muduo/net/TcpConnection.h>
#include <unordered_map>
#include <functional>
#include <mutex>


using namespace std;
using namespace muduo;
using namespace muduo::net;

using MsgHandler = std::function<void(const TcpConnectionPtr&, json& js, Timestamp)>;

//聊天器服务类
class ChatService
{
public:

    //description: 获取单例对象的接口函数
    //Date: 2024-06-15 11:13:02
    static ChatService* getInstance();

    //description: 处理登录业务
    //param conn: 服务器连接返回值
    //param js: 收到的json数据
    //param time: 收到消息的时间
    //return {void}
    //Date: 2024-06-15 11:14:15
    void login(const TcpConnectionPtr& conn, json& js, Timestamp time);

    //description: 处理注销业务
    //param &conn:
    //param &js:
    //param time:
    //return {void}
    //Date: 2024-06-27 17:27:29
    void logout(const TcpConnectionPtr &conn, json &js, Timestamp time);

    //description: 处理注册业务
    //param conn: 服务器连接返回值
    //param js: 收到的json数据
    //param time: 收到消息的时间
    //return {void}
    //Date: 2024-06-15 11:16:31
    void reg(const TcpConnectionPtr& conn, json& js, Timestamp time);

    //description: 一对一聊天业务
    //param conn:服务器连接返回值
    //param js:
    //param time:
    //return {void}
    //Date: 2024-06-18 13:43:45
    void oneChat(const TcpConnectionPtr& conn, json& js, Timestamp time);

    //description: 添加好友业务
    //Date: 2024-06-19 17:45:29
    void addFriend(const TcpConnectionPtr& conn, json& js, Timestamp time);

    //description: 创建群组并且把自己加入该群组并且为群主业务
    //param conn:
    //param js:
    //param time:
    //return {void}
    //Date: 2024-06-22 22:41:41
    void createGroup(const TcpConnectionPtr& conn, json& js, Timestamp time);

    //description: 加入群组业务
    //param conn:
    //param js:
    //param time:
    //return {void}
    //Date: 2024-06-22 23:36:24
    void addGroup(const TcpConnectionPtr& conn, json& js, Timestamp time);

    //description: 群组聊天业务
    //param conn:
    //param js:
    //param time:
    //return {void}
    //Date: 2024-06-23 11:03:53
    void groupChat(const TcpConnectionPtr& conn, json& js, Timestamp time);

    //description: 获取消息对应的处理器
    //param msgid: 收到的消息类型
    //return {void}
    //Date: 2024-06-15 13:36:44
    MsgHandler getHandler(int msgid);

    //description: 处理客户端异常退出
    //param conn:
    //return {void}
    //Date: 2024-06-17 22:48:01
    void clientCloseException(const TcpConnectionPtr& conn);

    //description: 服务器异常退出（收到退出信号）,业务处理函数
    //Date: 2024-06-19 10:26:04
    void reset();

    // 从redis消息队列中获取订阅的消息
    void handleRedisSubscribeMessage(int userid, string msg);
private:

    ChatService();

    //存储消息id和其对应的业务处理方法
    unordered_map<int, MsgHandler> msgHandlerMap_;

    //存储在线用户的通信连接
    unordered_map<int,TcpConnectionPtr> userConnMap_;

    //定义互斥锁，保证userConnMap的安全
    mutex connMutex_;

    //数据操作类
    UserModel userModel_;
    //离线消息操作类
    OfflineMsgModel offlineMsgModel_;
    //好友信息操作类
    FriendModel friendModel_;
    //群组信息操作类
    GroupModel groupModel_;

    //redis操作对象
    Redis redis_;
};